package com.linkit.main.setting;

import java.util.List;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkit.config.Setting;
import com.linkit.main.MainActivity;
import com.linkit.main.R;
import com.linkit.model.Category;
import com.linkit.utils.ScreenUtils;
import com.linkit.utils.Utils;

/**
 * adapter hiện thị danh sách các hãng xe trong tuyến
 * */
public class CategoriesAdapter extends BaseAdapter implements OnItemClickListener{

	private MainActivity context;
	private List<Category> items;
	private List<String> ids;
	private boolean isSelectedAll;

	public CategoriesAdapter(List<Category> items) {
		this.context = MainActivity.getInstance();
		this.items = items;
		ids = Utils.parseString(Setting.getCatsId());
		isSelectedAll = Utils.isSelectedAll(ids);
	}

	public class ViewHolder {
		private TextView domain;
		private ImageView imageView;
		private Category category;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		Category rowItem = getItem(position);

		LayoutInflater mInflater = context.getLayoutInflater();
		if (convertView == null) {
			convertView = mInflater.inflate(
					R.layout.setting_categories_item, null);
			holder = new ViewHolder();
			holder.domain = (TextView) convertView
					.findViewById(R.id.setting_site_domain);
			
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.setting_site_selected);
			convertView.setTag(holder);
			
		} else{
			holder = (ViewHolder) convertView.getTag();
		}
			

		holder.domain.setText(rowItem.name);
		holder.domain.setTextSize(ScreenUtils.getDialogItemFontSize());
		holder.category = rowItem;
		
		int resId = R.drawable.checkbox;
		rowItem.isSelected = false;
		
		if (isSelectedAll) {
			resId = R.drawable.checkboxfocus;
			rowItem.isSelected = true;
		} else if (ids != null) {
			int id;
			for (int i = 0; i < ids.size(); i++) {
				id = Integer.parseInt(ids.get(i));
				if (id == holder.category.id) {
					resId = R.drawable.checkboxfocus;
					rowItem.isSelected = true;
					break;
				}
			}

		}
		holder.imageView.setImageResource(resId);
		
		return convertView;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Category getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		ViewHolder holder = (ViewHolder)view.getTag();
		holder.category.isSelected = !holder.category.isSelected;
		
		if(holder.category.isSelected){
			holder.imageView.setImageResource(R.drawable.checkboxfocus);
		}else{
			holder.imageView.setImageResource(R.drawable.checkbox);
		}
		
		// Lưu config
		StringBuffer sBuffer = new StringBuffer("");
		Category cat;
		for (int i = 0; i < getCount(); i++) {
			cat = getItem(i);
			if (cat.isSelected) {
				sBuffer.append(cat.id + Setting.SPACE);
			}
		}

		if (sBuffer.length() >= 1) {
			Setting.setCatsId(sBuffer.substring(0, sBuffer.length() - 1));
		} else {
			Setting.setCatsId("");
		}

		Log.e("", "cat ===" + Setting.getCatsId());
	}
}